<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- dynamicview-tutorial.qdoc -->
  <title>QML Dynamic View Ordering Tutorial 1 - A Simple ListView and Delegate | Qt Quick 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtquick-index.html">Qt Quick 模块</a></td><td >QML 动态视图排序教程 1 - 一个简单的 ListView 和 Delegate</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtquick-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="qml-dynamicview-tutorial.html" />
  <link rel="next" href="qtquick-tutorials-dynamicview-dynamicview2-example.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-dynamicview-tutorial.html">QML 动态视图排序教程</a>
<span class="naviSeparator">  &#9702;  
<a class="nextPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-dynamicview-dynamicview2-example.html">QML 动态视图排序教程 2 - 拖动视图项</a></span>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level2"><a href="#walkthrough">Walkthrough</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QML 动态视图排序教程 1 - 一个简单的 ListView 和 Delegate</h1>
<span class="subtitle"></span>
<!-- $$$tutorials/dynamicview/dynamicview1-description -->
<div class="descr"> <a name="details"></a>
<p>我们通过定义一个<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-listview.html">ListView</a>开始我们的应用程序，该模型将向视图提供数据，以及一个委托，它提供用于在视图中构造项目的模板。</p>
<p><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-listview.html">ListView</a>和委托的代码如下所示：</p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
      <span class="name">id</span>: <span class="name">root</span>

      <span class="name">width</span>: <span class="number">300</span>; <span class="name">height</span>: <span class="number">400</span>

      <span class="type"><a href="../qtqml/qml-qtqml-component.html">Component</a></span> {
          <span class="name">id</span>: <span class="name">dragDelegate</span>

          <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
              <span class="name">id</span>: <span class="name">content</span>

              <span class="type">anchors</span> { <span class="name">left</span>: <span class="name">parent</span>.<span class="name">left</span>; <span class="name">right</span>: <span class="name">parent</span>.<span class="name">right</span> }
              <span class="name">height</span>: <span class="name">column</span>.<span class="name">implicitHeight</span> <span class="operator">+</span> <span class="number">4</span>

              <span class="name">border</span>.width: <span class="number">1</span>
              <span class="name">border</span>.color: <span class="string">&quot;lightsteelblue&quot;</span>

              <span class="name">radius</span>: <span class="number">2</span>

              <span class="type"><a href="qml-qtquick-column.html">Column</a></span> {
                  <span class="name">id</span>: <span class="name">column</span>
                  <span class="type">anchors</span> { <span class="name">fill</span>: <span class="name">parent</span>; <span class="name">margins</span>: <span class="number">2</span> }

                  <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Name: '</span> <span class="operator">+</span> <span class="name">name</span> }
                  <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Type: '</span> <span class="operator">+</span> <span class="name">type</span> }
                  <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Age: '</span> <span class="operator">+</span> <span class="name">age</span> }
                  <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Size: '</span> <span class="operator">+</span> <span class="name">size</span> }
              }
          }
      }
      <span class="type"><a href="qml-qtquick-listview.html">ListView</a></span> {
          <span class="name">id</span>: <span class="name">view</span>

          <span class="type">anchors</span> { <span class="name">fill</span>: <span class="name">parent</span>; <span class="name">margins</span>: <span class="number">2</span> }

          <span class="name">model</span>: <span class="name">PetsModel</span> {}
          <span class="name">delegate</span>: <span class="name">dragDelegate</span>

          <span class="name">spacing</span>: <span class="number">4</span>
          <span class="name">cacheBuffer</span>: <span class="number">50</span>
      }
  }

</pre>
<p>该模型在一个单独的 QML 文件中定义，如下所示：</p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="../qtqml/qml-qtqml-models-listmodel.html">ListModel</a></span> {
      <span class="type"><a href="../qtqml/qml-qtqml-models-listelement.html">ListElement</a></span> {
          <span class="name">name</span>: <span class="string">&quot;Polly&quot;</span>
          <span class="name">type</span>: <span class="string">&quot;Parrot&quot;</span>
          <span class="name">age</span>: <span class="number">12</span>
          <span class="name">size</span>: <span class="string">&quot;Small&quot;</span>
      }
      <span class="type"><a href="../qtqml/qml-qtqml-models-listelement.html">ListElement</a></span> {
          <span class="name">name</span>: <span class="string">&quot;Penny&quot;</span>
          <span class="name">type</span>: <span class="string">&quot;Turtle&quot;</span>
          <span class="name">age</span>: <span class="number">4</span>
          <span class="name">size</span>: <span class="string">&quot;Small&quot;</span>
      }
  }

</pre>
<a name="walkthrough"></a>
<h3 >演练</h3>
<p>在应用程序的根矩形中定义的第一项是委托组件。<a href="qml-qtquick-listview.html">ListView</a>中的每一项都是由这个模板构建的。</p>
<p>委托中引用的<code>name</code>、<code>age</code>、<code>type</code>和<code>size</code>变量来自模型数据。这些名称对应于模型中定义的角色。</p>
<pre class="qml">

      <span class="type"><a href="../qtqml/qml-qtqml-component.html">Component</a></span> {
          <span class="name">id</span>: <span class="name">dragDelegate</span>

          <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> {
              <span class="name">id</span>: <span class="name">content</span>

              <span class="type">anchors</span> { <span class="name">left</span>: <span class="name">parent</span>.<span class="name">left</span>; <span class="name">right</span>: <span class="name">parent</span>.<span class="name">right</span> }
              <span class="name">height</span>: <span class="name">column</span>.<span class="name">implicitHeight</span> <span class="operator">+</span> <span class="number">4</span>

              <span class="name">border</span>.width: <span class="number">1</span>
              <span class="name">border</span>.color: <span class="string">&quot;lightsteelblue&quot;</span>

              <span class="name">radius</span>: <span class="number">2</span>

              <span class="type"><a href="qml-qtquick-column.html">Column</a></span> {
                  <span class="name">id</span>: <span class="name">column</span>
                  <span class="type">anchors</span> { <span class="name">fill</span>: <span class="name">parent</span>; <span class="name">margins</span>: <span class="number">2</span> }

                  <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Name: '</span> <span class="operator">+</span> <span class="name">name</span> }
                  <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Type: '</span> <span class="operator">+</span> <span class="name">type</span> }
                  <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Age: '</span> <span class="operator">+</span> <span class="name">age</span> }
                  <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">'Size: '</span> <span class="operator">+</span> <span class="name">size</span> }
              }
          }
      }

</pre>
<p>应用程序的第二部分是我们将模型和委托绑定到的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-qtquick-listview.html">ListView</a>本身。</p>
<pre class="qml">

      <span class="type"><a href="qml-qtquick-listview.html">ListView</a></span> {
          <span class="name">id</span>: <span class="name">view</span>

          <span class="type">anchors</span> { <span class="name">fill</span>: <span class="name">parent</span>; <span class="name">margins</span>: <span class="number">2</span> }

          <span class="name">model</span>: <span class="name">PetsModel</span> {}
          <span class="name">delegate</span>: <span class="name">dragDelegate</span>

          <span class="name">spacing</span>: <span class="number">4</span>
          <span class="name">cacheBuffer</span>: <span class="number">50</span>
      }

</pre>
<p>文件：</p>
<ul>
<li><a href="qtquick-tutorials-dynamicview-dynamicview1-petsmodel-qml.html">tutorials/dynamicview/dynamicview1/PetsModel.qml</a></li>
<li><a href="qtquick-tutorials-dynamicview-dynamicview1-dynamicview-qml.html">tutorials/dynamicview/dynamicview1/dynamicview.qml</a></li>
<li><a href="qtquick-tutorials-dynamicview-dynamicview1-dynamicview1-qmlproject.html">tutorials/dynamicview/dynamicview1/dynamicview1.qmlproject</a></li>
</ul>
</div>
<!-- @@@tutorials/dynamicview/dynamicview1 -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qml-dynamicview-tutorial.html">QML 动态视图排序教程</a>
<span class="naviSeparator">  &#9702;  
<a class="nextPage" href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtquick/qtquick-tutorials-dynamicview-dynamicview2-example.html">QML 动态视图排序教程 2 - 拖动视图项</a></span>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
